package me.yobol.greentable.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import me.yobol.greentable.bean.DishesBean;
import me.yobol.greentable.service.DishesService;

/**
 * @author Yobol
 *
 */
public class ModifyDishesServlet extends HttpServlet {

	private static final long serialVersionUID = -2760957270898586832L;

	public ModifyDishesServlet() {
		super();
	}

	public void init() throws ServletException {
		// Put your code here
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int dishesId = (request.getParameter("dishesId") == null) ? -1 : Integer.parseInt(request.getParameter("dishesId"));
		
		if(dishesId > 0){
			DishesBean dishes = new DishesBean();
			String dishesName = request.getParameter("dishesName");
			dishes.setDishesName(dishesName);
			String dishesDescription = request.getParameter("dishesDescription");
			dishes.setDishesDescription(dishesDescription);
			String dishesContent = request.getParameter("dishesContent");
			dishes.setDishesContent(dishesContent);
			int dishesOwner = Integer.parseInt(request.getParameter("dishesOwner"));
			dishes.setDishesOwner(dishesOwner);
			int menuId = Integer.parseInt(request.getParameter("menuId"));
			dishes.setMenuId(menuId);
			
			if(DishesService.updateDishes(dishesId, dishes)){
				String forwardUrl = "/pages/modify_dishes.jsp?msg=modify_success";
			
				RequestDispatcher rd = request.getRequestDispatcher(forwardUrl);
				rd.forward(request, response);
			}
		}
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

}
